//package com.gzy.studentsociety.web;
//
//import com.gzy.studentsociety.dao.MemberDao;
//import com.gzy.studentsociety.dao.SocietyDao;
//import com.gzy.studentsociety.dao.StudentDao;
//import com.gzy.studentsociety.dao.impl.MemberDaoImpl;
//import com.gzy.studentsociety.dao.impl.StudentDaoImpl;
//import com.gzy.studentsociety.entity.Member;
//import com.gzy.studentsociety.entity.Student;
//import com.gzy.studentsociety.dao.impl.SocietyDaoImpl;
//import com.gzy.studentsociety.entity.Society;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//import java.sql.SQLException;
//import java.util.HashMap;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@WebServlet(name="memberServlet",value = "/member/*")
//public class MemberServlet extends RouteServlet{
//    private SocietyDao societyDao = new SocietyDaoImpl();
//
//    private MemberDao memberDao = new MemberDaoImpl();
//    private StudentDao studentDao = new StudentDaoImpl();
//
//    @Override
//    protected Class getMyClass() {
//        return this.getClass();
//    }
//    public Object addPage (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//        HttpSession session = req.getSession();
//        String number = (String)session.getAttribute("student");
//        Society society = null;
//        try {
//            society = societyDao.queryByStudentNumber(number);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//            handleException(throwables,req,res);
//        }
//        req.setAttribute("society",society);
//        return "member/addStudent";
//    }
//    public Object addMember(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
//        String stuIdStr = req.getParameter("stuId");
//        String assIdStr = req.getParameter("assId");
//        Integer stuId = Integer.valueOf(stuIdStr);
//        Integer assId = Integer.valueOf(assIdStr);
//        Member member = new Member();
//        member.setMember_societyId(String.valueOf(assId));
//        member.setMemberStudentId(stuId);
//        member.setJoinDate(new Date());
//
//        Integer result = null;
//        try {
//            int i = memberDao.isMemberIn(member);
//            if(i > 0){
//                HashMap<String,String> map = new HashMap<String,String>();
//                map.put("message","该生已加入");
//                return map;
//            }
//            result = memberDao.addMember(member);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//            handleException(throwables,req,res);
//        }
//        return result;
//    }
//
//    public Object members(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//        List<Member> memberList = null;
//        String statusStr = req.getParameter("status");
//        Integer status = Integer.valueOf(statusStr);
//        addPage(req, res);
//        Society soc = (Society)req.getAttribute("society");
//        Integer societyId = ((Society)req.getAttribute("society")).getSocietyId();
//        System.out.println(soc);
//        System.out.println(societyId);
//        System.out.println(status);
//        try {
//            memberList = memberDao.queryAllMember(status,societyId);
//            System.out.println(memberList);
//            memberList = memberList.stream().map(member -> {
//                Student stu = null;
//                try {
//                    stu = studentDao.findById(member.getMemberStudentId());
//                    System.out.println(member.getMemberStudentId());
//                    member.setMemberInfo(stu);
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//                return member;
//            }).collect(Collectors.toList());
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//            handleException(throwables,req,res);
//        }
//        req.setAttribute("members",memberList);
//        return status == 1 ? "member/memberList" : "member/quitList";
//    }
//
//    public Object deleteGraduated (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
//        String assIdStr = req.getParameter("assId");
//        Integer assId = Integer.valueOf(assIdStr);
//        Integer result = null;
//        try {
//            result = memberDao.deleteGraduated(assId);
//        }catch (SQLException throwables) {
//            throwables.printStackTrace();
//            handleException(throwables,req,res);
//        }
//        return result;
//    }
//
//    public Object deleteMember (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
//        String idStr = req.getParameter("id");
//        String statusStr = req.getParameter("status");
//        Integer id = Integer.parseInt(idStr);
//        Integer status = Integer.valueOf(statusStr);
//        System.out.println(id+" "+status);
//        Integer result = null;
//        try {
//            result = memberDao.deleteMember(id,status);
//        }catch (SQLException throwables) {
//            throwables.printStackTrace();
//            handleException(throwables,req,res);
//        }
//        return result;
//    }
//}



package com.gzy.studentsociety.web;
import com.gzy.studentsociety.dao.MemberDao;
import com.gzy.studentsociety.dao.SocietyDao;
import com.gzy.studentsociety.dao.StudentDao;
import com.gzy.studentsociety.dao.impl.MemberDaoImpl;
import com.gzy.studentsociety.dao.impl.StudentDaoImpl;
import com.gzy.studentsociety.entity.Member;
import com.gzy.studentsociety.entity.Student;
import com.gzy.studentsociety.dao.impl.SocietyDaoImpl;
import com.gzy.studentsociety.entity.Society;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

// 导入必要的Date类，用于处理日期相关操作
import java.util.Date;

@WebServlet(name = "memberServlet", value = "/member/*")
public class MemberServlet extends RouteServlet {

    // 正确创建接口类型的实例，确保SocietyDaoImpl正确实现了SocietyDao接口
    private SocietyDao societyDao = new SocietyDaoImpl();
    private MemberDao memberDao = new MemberDaoImpl();
    private StudentDao studentDao = new StudentDaoImpl();

    @Override
    protected Class getMyClass() {
        return this.getClass();
    }

    public Object addPage(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String number = (String) session.getAttribute("student");
        Society society = null;
        try {
            society = societyDao.queryByStudentNumber(number);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, req, res);
        }
        req.setAttribute("society", society);
        return "/member/addStudent";
    }

    public Object addMember(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String stuIdStr = req.getParameter("stuId");
        System.out.println(stuIdStr);
        String assIdStr = req.getParameter("assId");
        System.out.println(assIdStr);

        // 验证stuIdStr是否为有效的数字字符串且不为null
        if (stuIdStr == null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("message", "学生ID不能为空，请检查输入");
            return map;
        }
        if (stuIdStr == null || "".equals(stuIdStr) ||!isNumeric(stuIdStr)) {
            HashMap<String, String> map = new HashMap<>();
            map.put("message", "学生ID无效，请检查输入");
            return map;
        }

        // 验证assIdStr是否为有效的数字字符串且不为null
        if (assIdStr == null) {
            HashMap<String, String> map = new HashMap<>();
            map.put("message", "协会ID不能为空，请检查输入");
            return map;
        }
        if (assIdStr == null || "".equals(assIdStr) ||!isNumeric(assIdStr)) {
            HashMap<String, String> map = new HashMap<>();
            map.put("message", "协会ID无效，请检查输入");
            return map;
        }

        Integer stuId = Integer.valueOf(stuIdStr);
        Integer assId = Integer.valueOf(assIdStr);

        // 假设Member类已正确定义了相关属性和方法
        Member member = new Member();
        member.setMember_societyId(String.valueOf(assId));
        member.setMemberStudentId(stuId);
        member.setJoinDate(new Date());

        Integer result = null;
        try {
            int i = memberDao.isMemberIn(member);
            if (i > 0) {
                HashMap<String, String> map = new HashMap<>();
                map.put("message", "该生已加入");
                return map;
            }
            result = memberDao.addMember(member);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, req, res);
        }
        return result;
    }
    // 辅助方法，用于判断一个字符串是否为有效的数字字符串
    private boolean isNumeric(String str) {
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public Object members(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        List<Member> memberList = null;
        String statusStr = req.getParameter("status");
        Integer status = Integer.valueOf(statusStr);

        addPage(req, res);
        Society soc = (Society) req.getAttribute("society");
        Integer societyId = ((Society) req.getAttribute("society")).getSocietyId();
        System.out.println(soc);
        System.out.println(societyId);
        System.out.println(status);

        try {
            memberList = memberDao.queryAllMember(status, societyId);
            System.out.println(memberList);
            memberList = memberList.stream().map(member -> {
                Student stu = null;
                try {
                    stu = studentDao.findById(member.getMemberStudentId());
                    System.out.println(member.getMemberStudentId());
                    member.setMemberInfo(stu);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                return member;
            }).collect(Collectors.toList());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, req, res);
        }
        req.setAttribute("members", memberList);
        return status == 1? "member/memberList" : "member/quitList";
    }

    public Object deleteGraduated(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String assIdStr = req.getParameter("assId");
        Integer assId = Integer.valueOf(assIdStr);
        Integer result = null;
        try {
            result = memberDao.deleteGraduated(assId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, req, res);
        }
        return result;
    }

    public Object deleteMember(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String idStr = req.getParameter("id");
        String statusStr = req.getParameter("status");
        Integer id = Integer.parseInt(idStr);
        Integer status = Integer.valueOf(statusStr);
        System.out.println(id + " " + status);
        Integer result = null;
        try {
            result = memberDao.deleteMember(id, status);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, req, res);
        }
        return result;
    }
}